package leetcode;

/**
 * @author ht
 * @create 2025-06-18 20:19
 * @desc
 **/
public class T27 {

    /**
     * 暴力解法
     * @param nums
     * @param val
     * @return
     */
    public int removeElement(int[] nums, int val) {
        int size = nums.length;
        for (int i = 0; i < size; i++) {
            if(nums[i] == val) {
                for (int j = i + 1; j < size; j++) {
                    nums[j - 1] = nums[j];
                }
                i--;
                size--;
            }
        }
        return size;
    }

    /**
     * 双指针写法
     * @param nums
     * @param val
     * @return
     */
    public int removeElement2(int[] nums, int val) {
        int left = 0;
        for (int i = 0; i < nums.length; i++) {
            if(nums[i] != val) {
                nums[left++] = nums[i];
            }
        }
        return left;
    }

}
